<template>
	<view>
		<view class="goods-list" @click="gotoDetail">
			<view v-for="(item, index) in goodsList" :key="index">
				<my-goods :goods="item" :data-goods_id="item.goods_id"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj: {
					query: '',
					cid: '',
					pagenum: 1,
					pagesize: 10
				},
				goodsList: [],
				total: 0,
				isLoading: false
			};
		},
		onLoad(options) {
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			this.getGoodsList()
		},
		onReachBottom() {
			if (this.queryObj.pagesize * this.queryObj.pagenum >= this.total) return uni.$showmsg('数据加载完毕')

			if (this.isLoading) return
			this.queryObj.pagenum += 1
			console.log(this.queryObj.pagenum)
			this.getGoodsList()
		},
		onPullDownRefresh() {
			this.queryObj.pagenum = 1
			this.isLoading = false
			this.total = 0
			this.goodsList = []
			this.getGoodsList(() => uni.stopPullDownRefresh())
		},
		methods: {
			async getGoodsList(callback) {
				this.isLoading = true
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
				callback && callback()
				if (res.meta.status !== 200) return uni.$showmsg()
				this.goodsList = [...this.goodsList, ...res.message.goods]
				this.total = res.message.total
				this.isLoading = false
			},
			gotoDetail(e){
				uni.navigateTo({
					url:`/subpkg/goods_detail/goods_detail?goods_id=${e.target.dataset.goods_id}`
				})
			}
		}
	}
</script>

<style lang="scss">

</style>
